<?php
namespace app\admin\controller;

use think\Controller;
use think\Loader;
use think\Request;
use app\common\model\Tips as TipsModel;

class Agent extends Controller{

    public function index(){
        $history = TipsModel::order('id desc')->where('type',7)->paginate(20);

        return view('',['history'=>$history]);
    }

    public function add(){

        return view();
    }

    public function create( Request $request ){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['title','desc','cover','content']);
        $data['type'] = 7;
        $tipsModel = new TipsModel();

        $validate = Loader::validate('TipsValidate');
        $result = $validate-> scene('agentAdd')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $tipsModel = new TipsModel();
        $tipsModel -> save($data);

        $this->success("添加成功");
    }


    public function edit(Request $request){

        $id = $request->param('id',0,'intval');
        $history = TipsModel::find($id);

        return view('',['history'=>$history]);
    }


    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法操作");
        }

        $data = $request -> only(['title','desc','cover','content']);
        $id = $request -> param('id',0,'intval');
        if(!$id){
            $this->error("非法操作");
        }

        $validate = Loader::validate('TipsValidate');
        $result = $validate-> scene('agentEdit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $tipsModel = TipsModel::find($id);
        $tipsModel -> save($data);

        $this->success("更新成功");
    }


    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法操作");
        }
        $id = $request -> param('id');
        TipsModel::destroy($id);
        $this->success("删除成功");
    }

}
